package com.gl.rent;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.gl.common.util.SecurityUtils;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.HotAttractions;
import com.haixiaoke.saas.repository.service.IHotAttractionsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 热门景点Controller
 *
 * @author qixi
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/hot/attractions")
public class HotAttractionsController extends BaseController {

    @Autowired
    private  IHotAttractionsService hotAttractionsService;

    /**
     * 查询热门景点列表
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:attractions:list')")
    @GetMapping("/list")
    public TableDataInfo list(HotAttractions hotAttractions) {
        startPage();
        List<HotAttractions> list = hotAttractionsService.selectHotAttractionsList(hotAttractions);
        return getDataTable(list);
    }

    /**
     * 获取热门景点详细信息
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:attractions:query')")
    @GetMapping(value = "/{attractionsId}")
    public AjaxResult getInfo(@PathVariable("attractionsId") Long attractionsId) {
        return success(hotAttractionsService.selectHotAttractionsByAttractionsId(attractionsId));
    }

    /**
     * 新增热门景点
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:attractions:add')")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody HotAttractions hotAttractions) {
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        hotAttractions.setCreateBy(id);

        //校验坐标唯一
        if (!hotAttractionsService.checkCoordinatesUnique(hotAttractions)) {
            return error("景点已存在");
        }
        return toAjax(hotAttractionsService.insertHotAttractions(hotAttractions));
    }

    /**
     * 修改热门景点
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:attractions:edit')")
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody HotAttractions hotAttractions) {
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        hotAttractions.setUpdateBy(id);

        //校验坐标唯一
        HotAttractions original = hotAttractionsService.selectHotAttractionsByAttractionsId(hotAttractions.getAttractionsId());
        if (!ObjectUtil.equals(hotAttractions.getLongitude(), original.getLongitude()) ||
                !ObjectUtil.equals(hotAttractions.getLatitude(), original.getLatitude())) {
            if (!hotAttractionsService.checkCoordinatesUnique(hotAttractions)) {
                return error("景点已存在");
            }
        }
        return toAjax(hotAttractionsService.updateHotAttractions(hotAttractions));
    }

    /**
     * 删除热门景点
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:attractions:remove')")
    @DeleteMapping("/{attractionsIds}")
    public AjaxResult remove(@PathVariable Long[] attractionsIds) {
        return toAjax(hotAttractionsService.deleteHotAttractionsByAttractionsIds(attractionsIds));
    }
}
